/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/util/util.c,v 1.2 2002/07/19 04:27:18 bboy Exp $
	util.c: Routines shared by the utility programs.

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "util.h"

CONF *Conf;															/* Configuration data */
MYSQL *mysql;														/* MySQL connection */


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	LOAD_CONFIG
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
load_config(void)
{
	conf_load(&Conf, MYDNS_CONF);
}
/*--- load_config() -----------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	DB_CONNECT
	Connect to the database.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
db_connect(void)
{
	u_char *user = conf_get(&Conf, "mysql-user");
	u_char *host = conf_get(&Conf, "mysql-host");
	u_char *database = conf_get(&Conf, "database");
	u_char *password = conf_get(&Conf, "mysql-password");

	if (!(mysql = mysql_init(NULL)))
		Err(_("unable to allocate MySQL data structure"));

	if (!(mysql_real_connect(mysql, host, user, password, database, 0, NULL, 0)))
		ErrSQL(mysql, _("error connecting to MySQL server at %s"), host);

	Verbose(_("connected to MySQL on %s, db \"%s\""), host, database);
}
/*--- db_connect() ------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	SQLNUM
	Issues a query and returns the first column of the first row as a number.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
u_int32_t
sqlnum(const char *fmt, ...)
{
	va_list ap;
	char query[BUFSIZ];
	MYSQL_RES *res = NULL;
	MYSQL_ROW row;
	u_int32_t rv = 0;

	va_start(ap, fmt);
	vsnprintf(query, sizeof(query), fmt, ap);
	va_end(ap);

	if (mysql_real_query(mysql, query, strlen(query)) || !(res = mysql_store_result(mysql)))
		ErrSQL(mysql, _("error issuing query"));

	/* Add results to list */
	if ((row = mysql_fetch_row(res)))
		rv = atou(row[0]);
	mysql_free_result(res);
	return (rv);
}
/*--- sqlnum() ----------------------------------------------------------------------------------*/

/* vi:set ts=3: */
